package com.systop.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systop.tea.common.R;
import com.systop.tea.entity.Banner;
import com.systop.tea.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/banner")
public class BannerController {

    private final BannerService bannerService;

    @GetMapping("/query")
    public R<Page> query(Banner banner, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(banner.getBannerDec()), Banner::getBannerDec, banner.getBannerDec());
        queryWrapper.eq(banner.getBannerStatus() != null, Banner::getBannerStatus, banner.getBannerStatus());
        bannerService.page(page, queryWrapper);

        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<Banner> queryById(@PathVariable Integer id) {
        Banner banner = bannerService.getById(id);
        return R.success(banner);
    }

    @PostMapping
    public R<String> add(@RequestBody Banner banner) {
        boolean row = bannerService.save(banner);
        if (row) {
            return R.success("轮播图添加成功");
        } else {
            return R.error("轮播图添加失败");
        }
    }

    @PutMapping
    public R<String> modify(@RequestBody Banner banner) {
        boolean row = bannerService.updateById(banner);
        if (row) {
            return R.success("轮播图修改成功");
        } else {
            return R.error("轮播图修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        boolean rows = bannerService.removeById(id);
        if (rows) {
            return R.success("轮播图删除成功");
        } else {
            return R.error("轮播图删除失败");
        }
    }

}
